<?php

namespace Home\Controller;
use Think\Controller;
class StationInfoController extends BaseController{
    
    /**
     * @desc 获取站点信息通知方式接口
     * @author quyang
     * @update 2016-11-24
     */
    public function queryStationInfo(){
        $data = I('param.');
        if(empty($data['stationId'])){
            $this->param_error();
        }
        $where = array('f_user_id'=>$data['stationId']);
        $arr = D('Home/User')->stationinfo($where);
        if($arr == false){
            $this->output(0);
        }
        $this->output(1,$arr);
    }
    
    /**
     * @desc 短信统计接口
     * @author quyang
     * @again quyang 2017-1-18
     * @update 2016-11-24
     */
    public function querySmsComboByUser(){
        $data = I('param.');
        if(empty($data['stationId'])){
            $this->param_error();
        }
        $where = array('f_user_id'=>$data['stationId']);
        $arr = D('Home/User')->countsms($where,$data['stationId']);
        if($arr == false){
            $this->output(false);
        }
        $this->output(1,$arr);
    }
    
    /**
     * @desc 站点合作/取消合作接口
     * @author quyang
     * @update 2016-12-26
     */
    public function changeExpBranState(){
        $data = I('param.');
        if (empty($data['fExpId']) || $data['status'] == '' || empty($data['stationId'])){
            $this->param_error();
        }

        $User = new \Home\Model\UserModel();
        $courier = new \Home\Model\ExpressBranchComDzModel();

        //查询快递员快递公司信息
        $userInfo = $User->userInformation( ['f_user_id'=>$data['fExpId']] );

        if ( $data['status'] == 0 )
        {
            //查询相同快递是否已经存在
            $company = $courier->cooperationExpress( $data['stationId'] , $userInfo['f_company_id'] );
            if ( $company ){
                $this->auto_error( '相同的快递公司已存在' );
            }
        }

        $arr = $courier->changeBranchState($data['fExpId'],$data['status'],$data['stationId']);
        if ($arr === false){
            $this->output(0);
        }
        $this->output(1);
    }

    /**
     * @desc 获取合作快递员接口
     * @author quyang
     * @update 2016-12-26
     */
    public function queryCourier(){
        $data = I('param.');
        if (empty($data['stationId'])){
            $this->param_error();
        }
        $courier = new \Home\Model\ExpressBranchComDzModel();
        $page = empty($data['page']) ? 1:$data['page'];  //当前页
        $arr = $courier->queryCourier($data['stationId'],$page,10);
        if ($arr == false){
            $this->output(0);
        }
        $this->output(1,$arr);
    }

    /**
     * Function continual_use
     *
     * Author: 黄盖
     * Date: 2017-3-15 13:43
     * Info: 站点禁止/允许快递员入库
     */
    public function continual_use()
    {
        $data = I('param.');
        $stationId = intval( $data['stationId'] );
        $expId = intval( $data['expId'] );
        $status = intval( $data['status'] );

        if( $stationId > 0 && $expId > 0 )
        {
            $status = $status == -1 ? -1 : 0;

            //修改数据
            if( ( new \Home\Model\ExpressBranchComDzModel() ) ->continual_use( $stationId, $expId, $status ) )
            {
                $this->output(1);
            }
            else
            {
                $this ->auto_error('数据异常');
            }
        }

        $this->param_error();
    }

    /**
     * Function modifyPhone
     *
     * Author: quyang
     * Date: 2017-2-5 11:04
     * Info: 修改联系方式
     */
    public function modifyPhone(){

        $param = I('param.');

        if (empty($param['phone']) || empty($param['stationId'])){
            $this->param_error();
        }

        if(!preg_match("/^[1][3,4,5,6,7,8]\d{9}$/", $param['phone'])){
            $this->auto_error('请填写正确的手机号');
        }

        $user = new \Home\Model\UserModel();
        $data = $user->updatePhone($param);

        if ($data === false){
            $this->auto_error('修改失败');
        }

        //修改redis
        $server = new \Home\Server\ParcelService();
        $server->delRedis_UserInfo( $param['stationId'] );

        $this->output(true ,array() ,'修改成功');

    }

    /**
     * Function modifyPassword
     *
     * Author: quyang
     * Date: 2017-2-5 11:27
     * Info: 修改密码
     */
    public function modifyPassword(){

        $param = I('param.');

        if (empty($param['mobile']) || empty($param['password']) || $param['operationType'] == ''){
            $this->param_error();
        }

        //判断operationType类型为1时，操作类型为修改密码，userId必传
        //operationType类型为0时,则是找回密码操作
        if ( $param['operationType'] == 1 && empty($param['userId']) )
        {
            $this->param_error();
        }

        $user = new \Home\Model\UserModel();

        $data = $user->updatePassword($param);

        if ($data == 0){
            $this->auto_error('密码修改失败');
        }

        $this->output(true ,array() ,'修改成功');

    }

    /**
     * Function stationCooperation
     *
     * Author: quyang
     * Date: 2017-2-7 10:25
     * Info: 站点已合作，申请中快递
     */
    public function stationCooperation(){

        $param = I('param.');

        if (empty($param['stationId']) || $param['status'] == ''){
            $this->param_error();
        }

        $stationInfo = new \Home\Model\ExpressBranchComDzModel();
        $data = $stationInfo->getCooperationCompany($param);

        $this->output(true,$data);

    }

    /**
     * @desc 获取站点下收件人手机号
     * @author quyang
     * @update 2017-02-17
     */
    public function queryMobileInfo(){

        $param = I('param.');
        if ( empty($param['userId']) ){
            $this->param_error();
        }

        $mobile = new \Home\Model\MobileInfoModel();

        $data = $mobile->mobileInfo( $param['userId'] , 1 , 1000000 , '' , '' , [0,-1] );

        $this->output(true,$data);

    }

    /**
     * @desc  : 根据手机号查询收件人信息
     * @author：quyang
     * @param ：
     * @time  ：2017/2/20 9:43
     * @update：2017/2/20
     **/
    public function queryStationMobile()
    {

        $param = I('param.');

        if ( empty($param['mobile']) || empty($param['userId']) ){
            $this->param_error();
        }

        $mobile = new \Home\Model\MobileInfoModel();

        $data = $mobile->queryMobile( $param['userId'] ,$param['mobile'] );

        $this->output(true,$data);

    }

    /**
     * @Desc  : 更新包裹状态（2、5可更改包裹状态）
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/21 14:51
     * @Update：2017/3/21
     **/
    public function changeParcelStatus()
    {

        $param = I('param.');
        //get参数
        $getData = I('get.');

        if ( empty( $param['parcelId'] ) && empty( $param['stationId'] ) && empty( $param['tableName'] ) && $param['status'] == '' && empty( $getData['bmxSource'] ) )
        {
            $this->param_error();
        }

        //记录是通过什么客户端修改的包裹状态
        $inputSource = '9';
        switch ( $getData['bmxSource'] )
        {
            case 'android':
                $inputSource = '-3';
                break;
            case 'ios':
                $inputSource = '-2';
                break;
            case 'pc':
                $inputSource = '-1';
                break;
        }

        $where = array(
            'f_parcel_id'   =>  $param['parcelId'],
            'f_station_id'  =>  $param['stationId']
        );
        $where['f_parcel_status'] = array( 'in' , array('2','5') );

        $Array = array(
            'f_parcel_status'   =>  $param['status'],
            'f_last_time'       =>  date('YmdHis'),
            'f_last_timestamp'  =>  time(),
            'f_input_source'    =>  $inputSource
        );

        $ParcelInfo = new \Home\Server\QueryParcelService();
        $data = $ParcelInfo->updateParcel( $where , $Array ,$param['tableName'] );

        if ( $data == false )
        {
            $this->output(false);
        }
        $this->output(true);

    }

}